﻿
@{
    Layout = null;
}

<link href="~/libs/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />
<style>
	.layui-form-item {
		margin-bottom: 30px; /* 每个行之间的距离 */
		width: 300px
	}

	.layui-form-label {
		width: 160px
	}

	.layui-input-block {
		margin-left: 200px;
		width: 200px;
	}
</style>

<form class="layui-form" action="" style="margin-top:20px">
	<table>
		<tr>
			<td>
				<div class="layui-form-item">
					<label class="layui-form-label">分类编码</label>
					<div class="layui-input-block">
						<input type="text" name="ProductCode" lay-verify="required" placeholder="请输入分类编码" autocomplete="off" class="layui-input">
						<input type="checkbox" name="autoGenerate" lay-skin="switch" lay-filter="switchTest">自动生成
					</div>
				</div>
			</td>
		</tr>
		<tr>
			<td>
				<div class="layui-form-item">
					<label class="layui-form-label">分类名称</label>
					<div class="layui-input-block">
						<input type="text" name="ProductNam" lay-verify="required" placeholder="请输入分类名称" autocomplete="off" class="layui-input">
					</div>
				</div>
			</td>
		</tr>
		<tr>
			<td>
				<div class="layui-form-item">
					<label class="layui-form-label">分类级别</label>
					<div class="layui-input-block">
						<input type="number" name="Productlevel" placeholder="" autocomplete="off" class="layui-input" min="0" step="1" lay-affix="number">
					</div>
				</div>
			</td>
		</tr>
		<tr>
			<td>
				<div class="layui-form-item">
					<label class="layui-form-label">父级主键</label>
					<div class="layui-input-block">
						<input type="number" name="Bld" placeholder="" autocomplete="off" class="layui-input" min="0" step="1" lay-affix="number">
					</div>
				</div>
			</td>
		</tr>
	</table>
	<table style="margin-bottom:50px">
		<div style="margin-left:150px;margin-top:30px">
			<button type="submit" class="layui-btn" lay-submit lay-filter="add">立即提交</button>

			<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		</div>
	</table>
</form>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.9.21/layui/layui.js"></script>
<script>
	layui.use(['form', 'layer'], function () {
		var form = layui.form;
		var layer = layui.layer;

		// 监听复选框变化，自动生成客户编码
		form.on('switch(switchTest)', function (data) {
			if (data.elem.checked) {
				// 生成随机字母+数字的编码并设置到CustomerCode输入框中
				var randomNum = Math.floor(Math.random() * 10) + '';
				for (var i = 0; i < 9; i++) {
					randomNum += Math.floor(Math.random() * 10);
				}
				randomNum = 'C' + randomNum;
				$('input[name="ProductCode"]').val(randomNum);

			}
			else
			{
				$('input[name="ProductCode"]').val('');
			}
		});


		form.on('submit(add)', function (data) {
			var field = data.field; // 获取表单字段值

			// 此处可执行 Ajax 等操作
			$.ajax({
				url:"/Productcategory/CreateProducate",
				type:"post",
				async:true,
				data: field,
				datatype: "json",
				success: function (res) {
					console.log(res)
					if (res.code == 1) {
						layer.alert('添加成功', { title: '操作结果' }, function () {
							var index = parent.layer.getFrameIndex(window.name); // 获取当前 iframe 层的索引
							parent.layer.close(index); // 关闭当前 iframe 层
							parent.location.reload(); // 刷新父页面
						});
					}
					else{
						layer.alert('添加失败，已有该编码', { title: '操作结果' });
					}
				}
			});
			return false; // 阻止默认 form 跳转
		})
	})
</script>
